var App = getApp()

/**
 * 小程序配置文件
 */
// let HOST = 'http://192.168.10.36:8866'
let HOST = 'https://usen.nihaoreading.cn'


/**
 * POST请求
 */
function post(url, data) {
  console.log("------------post请求-------------")
  console.log("|请求url:" + url)
  console.log("|请求参数:" + data)
  console.log("--------------------------------")

  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 0 || res.data.code == -2) {
            resolve(res)
          } else {
            App.showError(res.data.errMsg); 
            // reject(res)
            resolve(res);
          }
        } else {
          reject(res)
        }
      },
      fail: function (error) {
        reject(error)
      }
    })
  })
}


/**
 * 用户授权登录
 */
function signIn(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/signIn", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 获取用户邀请的信息
 */
function getPersonalInformation(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/getUserInfo", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 获取搜索推荐数据
 */
function getSearchRecommend() {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/comm/searchRecommend").then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 创建空间
 */
function createSpace(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/spaces/save", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 获取搜索推荐数据
 */
function thumbsUpOperation(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/thumbsUpOperation", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 搜索
 */
function search(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/comm/search", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 获取用户所有圈子
 */
function getMySpaces(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/spaces/getMySpaces", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 获取话题详情数据
 */
function getTopicDetail(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/getTopicDetail", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 话题评论
 */
function commentTopic(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/commentTopic", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 根据栏目获取空间数据
 */
function getTopics(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/getTopics", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 是否已实名
 */
function isRealName(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/isRealName", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 圈主及空间信息
 */
function getSpaceInfo(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/getSpaceInfo", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



/**
 * 圈主及空间信息
 */
function demandHall(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/comm/demandHall", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 删除成员
 */
function delMember(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/delMember", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 加入空间
 */
function joinSpace(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/spaces/joinSpace", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}

/**
 * 空间话题分享
 */
function addShare(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/addShare", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 发布话题
 */
function publishTopic(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/publishTopic", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 成员操作-同意加入、拒绝加入
 */
function memberOperation(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/topics/memberOperation", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 查询用户是否有未读消息
 */
function isHaveUnReadMsg(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/isHaveUnReadMsg", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 消息列表
 */
function getMsgs(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/getMsgs", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}


/**
 * 实名认证
 */
function authentication(data) {
  return new Promise((resolve, reject) => {
    post(HOST + "/api/users/authentication", data).then(function (result) {
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}



module.exports.host = HOST;
module.exports.signIn = signIn;
module.exports.getPersonalInformation = getPersonalInformation;
module.exports.getSearchRecommend = getSearchRecommend;
module.exports.createSpace = createSpace;
module.exports.thumbsUpOperation = thumbsUpOperation;
module.exports.getMySpaces = getMySpaces;
module.exports.getTopicDetail = getTopicDetail;
module.exports.commentTopic = commentTopic;
module.exports.getTopics = getTopics;
module.exports.isRealName = isRealName;
module.exports.getSpaceInfo = getSpaceInfo;
module.exports.demandHall = demandHall;
module.exports.search = search;
module.exports.delMember = delMember;
module.exports.joinSpace = joinSpace;
module.exports.publishTopic = publishTopic;
module.exports.addShare = addShare;
module.exports.memberOperation = memberOperation;
module.exports.isHaveUnReadMsg = isHaveUnReadMsg;
module.exports.isHaveUnReadMsg = isHaveUnReadMsg;
module.exports.getMsgs = getMsgs;
module.exports.authentication = authentication;



